package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.QiniuUtils;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

/*
体检套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealContorller {

    @Autowired
    private JedisPool jedisPool;

    //文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestBody MultipartFile imgFile){
        System.out.println(imgFile);
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index - 1);

        String fileName = UUID.randomUUID().toString()+extention;

        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //将存入名称存入缓存
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }

    @Reference
    private SetmealService setmealService;

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try{
            setmealService.add(setmeal,checkgroupIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //条件分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

           return setmealService.findPage(queryPageBean);
    }



    @RequestMapping("/delete")
    public Result delete(Integer id){

        try{
            setmealService.delete(id);
            return new Result(true,"删除检查套餐成功");
        }catch (Exception e){
            return new Result(false,"删除检查套餐错误");
        }

    }
    @RequestMapping("/findById")
    public Result findById(Integer id){

        try{
            Setmeal setmeal=setmealService.findById(id);
            return new Result(true,"查询检查套餐成功",setmeal);
        }catch (Exception e){
            return new Result(false,"查询检查套餐错误");
        }

    }



    @RequestMapping("/findSetmealByCheckGroupId")
    public Result findSetmealByCheckGroupId(Integer id){

        try{
            Integer[] integers=setmealService.findSetmealByCheckGroupId(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,integers);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try{
            setmealService.edit(setmeal,checkgroupIds);
        }catch (Exception e){
            return new Result(false,"修改体检套餐失败");
        }
        return new Result(true,"修改体检套餐成功");
    }
}
